package cn.msn.org.controller;

import cn.msn.basic.util.AjaxResult;
import cn.msn.basic.util.PageList;
import cn.msn.org.domain.Employee;
import cn.msn.org.query.EmployeeQuery;
import cn.msn.org.service.IEmployeeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @description: Employee的后端接口
 * @author: Zth
 * @date: 2022/8/14 : 15:07
 * @version: ver1.0
 * @email 329994750@qq.com
 */
@RestController
@RequestMapping("/employee")
@Api(value = "员工的API",description="员工相关的CRUD功能")
public class EmployeeController {

    @Autowired
    private IEmployeeService employeeService;

    @ApiOperation(value = "查询所有" )
    @GetMapping
    public List<Employee> loadAll() {
        return employeeService.loadAll();
    }

    @ApiOperation(value = "通过ID查询" )
    @GetMapping("/{id}")
    public Employee loadById(@PathVariable("id")Long id) {
        return employeeService.loadById(id);
    }

    @ApiOperation(value = "通过ID删除" )
    @DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable("id")Long id) {
        try {
            employeeService.remove(id);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"删除失败");
        }
    }
    @ApiOperation(value = "添加或修改" )
    @PutMapping // 等价于：@RequestMapping(     method = {RequestMethod.Put}   )
    public AjaxResult addOrUpdate(@RequestBody Employee employee) {
        try {
            if (employee.getId()==null) {
                employeeService.save(employee);
            } else {
                employeeService.update(employee);
            }
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"操作失败");
        }
    }
    // 批量删除
    @ApiOperation(value = "批量删除接口")
    @PatchMapping
    public AjaxResult patchRemove(@RequestBody List<Long> ids) {
        try {
            employeeService.patchRemove(ids);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"删除失败");
        }
    }
    @ApiOperation(value = "分页查询" )
    @PostMapping // 等价于：@RequestMapping(     method = {RequestMethod.POST}   )
    public PageList<Employee> queryPage(@RequestBody EmployeeQuery employeeQuery) {
        return employeeService.queryPage(employeeQuery);
    }


    // 根据店铺ID查询店铺人员
    @GetMapping("/list/{id}")
    public List<Employee> findByShopId(@PathVariable("id") Long shopId) {
        return employeeService.findByShopId(shopId);
    }
}
